﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace com.codeplex.eatoutadvocate.DAL
{
    public class DataConnection
    {
        public DataConnection(string connectionString)
        {
            ConnectionString = connectionString;
        }
        /// <summary>
        /// DB Connection String
        /// </summary>
        private static string ConnectionString { get; set; }

        /// <summary>
        /// Checks if specified user exists in User table
        /// </summary>
        /// <param name="user">User name to check</param>
        /// <returns>
        /// True: User exists
        /// False: User doesn't exist
        /// </returns>
        public bool UserExists(string user)
        {
            //Get connection string
            string connStr = ConnectionString;
            //Establish Data connection
            EatOutAdvocateDataContext data = new EatOutAdvocateDataContext(connStr);
            int count = (from u in data.Users
                         where u.Email == user
                         select u).Count();
            if (count > 0)
                return true;
            else
                return false;
        }

        public List<string> GetRestaurants(string text)
        {
            text = text.ToLower();
            //Get connection string
            string connStr = ConnectionString;
            //Establish Data connection
            EatOutAdvocateDataContext data = new EatOutAdvocateDataContext(connStr);

            var query = from r in data.Restaurants 
                               where r.Name.ToLower().Contains(text)
                               select r.Name;

            List<string> restaurants = query.ToList();
            return restaurants;
        }


        public List<Restaurant> GetRestaurants()
        {
            //Get connection string
            string connStr = ConnectionString;
            //Establish Data connection
            EatOutAdvocateDataContext data = new EatOutAdvocateDataContext(connStr);
            var query = from r in data.Restaurants
                        select r;

            List<Restaurant> restaurants = query.ToList();
            return restaurants;
        }

        public void Store(int userID, Engine.EatOut eatout)
        {
            //Get connection string
            string connStr = ConnectionString;
            //Establish Data connection
            EatOutAdvocateDataContext data = new EatOutAdvocateDataContext(connStr);
            
            User user = (from u in data.Users
                           where u.ID.Equals(userID)
                           select u).First() as User;

            Restaurant restaurant = (from r in data.Restaurants
                                    where r.Name.Equals(eatout.Restaurant)
                                    select r).First() as Restaurant;
                                        

            EatOut eatOutDB = new EatOut();
            eatOutDB.User = user;
            eatOutDB.Mood = (int)eatout.Mood;
            eatOutDB.MoodLevel = eatout.MoodLevel;
            eatOutDB.Restaurant = restaurant;
            eatOutDB.Temperature = (int)eatout.Temperature;
            eatOutDB.Weather = (int)eatout.Weather;
            eatOutDB.TimeHour = eatout.Time;
            eatOutDB.TimeStamp = DateTime.Now;
                
            data.EatOuts.InsertOnSubmit(eatOutDB);
            data.SubmitChanges();
               
        }
  
    }
}
